﻿// This code is released under the BSD license.
namespace PugLib.UI.Controls
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public partial class PugProgressBar : UserControl
    {
        private bool _showPercentage;
        private string _text;

        public PugProgressBar()
        {
            InitializeComponent();
        }

        public int Minimum
        {
            get { return _bar.Minimum; }
            set { _bar.Minimum = value; }
        }

        public int Maximum
        {
            get { return _bar.Maximum; }
            set { _bar.Maximum = value; }
        }

        public int Value
        {
            get { return _bar.Value; }
            set
            {
                _bar.Value = value;
                Refresh();
            }
        }

        public int Step
        {
            get { return _bar.Step; }
            set { _bar.Step = value; }
        }

        public ProgressBarStyle BarStyle
        {
            get { return _bar.Style; }
            set { _bar.Style = value; }
        }

        public Color BarColor
        {
            get { return _bar.ForeColor; }
            set { _bar.ForeColor = value; }
        }

        public bool ShowPercentage
        {
            get { return _showPercentage; }
            set
            {
                _showPercentage = value;
                UpdateText();
            }
        }

        public string CenterText
        {
            get { return _text; }
            set
            {
                _text = value;
                UpdateText();
            }
        }

        public override void Refresh()
        {
            base.Refresh();
            UpdateText();
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            _bar.Size = Size;
            UpdateText();
        }

        protected override void OnLocationChanged(EventArgs e)
        {
            base.OnLocationChanged(e);
            UpdateText();
        }

        private void UpdateText()
        {
            string s;
            if (_showPercentage)
            {
                int percent = (int)(((Value - Minimum) / (double)(Maximum - Minimum)) * 100);
                s = percent + "%";
            }
            else
            {
                if (string.IsNullOrEmpty(_text))
                {
                    //Dont draw anything
                    return;
                }
                s = _text;
            }

            using (Graphics gr = _bar.CreateGraphics())
            {
                using (SolidBrush brush = new SolidBrush(ForeColor))
                {
                    int width = Width / 2;
                    int height = Height / 2;
                    SizeF stringSize = gr.MeasureString(s, Font);
                    float stringWidth = stringSize.Width / 2.0F;
                    float stringHeight = stringSize.Height / 2.0F;
                    PointF point = new PointF(width - stringWidth, height - stringHeight);
                    gr.DrawString(s, Font, brush, point);
                }
            }
        }
    }
}